import { createRouter, createWebHashHistory } from 'vue-router'

import UserListView from '@/views/user/ListView.vue'
import UserSaveView from '@/views/user/SaveView.vue'

import CategoryListView from '@/views/category/ListView.vue'

import BookListView from '@/views/book/ListView.vue'
import BookSaveView from '@/views/book/SaveView.vue'

import UnitListView from '@/views/unit/ListView.vue'
import UnitSaveView from '@/views/unit/SaveView.vue'

import WordListView from '@/views/word/ListView.vue'
import WordSaveView from '@/views/word/SaveView.vue'

import RateOfLearningListView from '@/views/RateOfLearning/ListView.vue'
const routes = [
  {
    path: '/',
    component: () => import('../views/layout/LayoutView.vue'),
    children:[
      {
        path: '/',
        redirect: '/user/list'
      },
      {
        path: '/user/list',
        name: 'userListView',
        component: UserListView
      },
      {
        path: '/user/save',
        name: 'userSaveView',
        component: UserSaveView
      },
      {
        path: '/category/list',
        name: 'categoryListView',
        component: CategoryListView
      },
      {
        path: '/book/list',
        name: 'bookListView',
        component: BookListView
      },
      {
        path: '/book/save',
        name: 'bookSaveView',
        component: BookSaveView
      },
      {
        path: '/unit/list',
        name: 'unitListView',
        component: UnitListView
      },
      {
        path: '/unit/save',
        name: 'unitSaveView',
        component: UnitSaveView
      },
      {
        path: '/word/list',
        name: 'wordListView',
        component: WordListView
      },
      {
        path: '/word/save',
        name: 'wordSaveView',
        component: WordSaveView
      },
      {
        path: '/RateOfLearning/list',
        name: 'rateOfLearningListView',
        component: RateOfLearningListView
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  const toPath = to.path
  if (toPath == '/login') {
    //判断是否已经登录过了
    if (token) {
      next({ path: '/user/list' })
    } else {
      next()
    }
  } else {
    //判断是否已经登录
    if (token) {
      //已经登录了
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

export default router
